package com.cloud.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: TongRui乀
 * @Date: 2019/10/18 15:14
 * @description： 自定义请求前过滤器
 */
@Slf4j
@Component
public class MyPreFilter extends ZuulFilter {

    /**
     *  过滤器类型
     *
     *  POST_TYPE = "post";
     *  PRE_TYPE = "pre";
     *  ROUTE_TYPE = "route";
     *
     * @return
     */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    /**
     *  过滤器顺序
     * @return
     */
    @Override
    public int filterOrder() {
        return 1;
    }

    /**
     *  哪些请求应该进入过滤器
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     *  过滤器逻辑
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {

        RequestContext context = RequestContext.getCurrentContext();

        HttpServletRequest request = context.getRequest();

        log.info(request.getRequestURI());

        return null;
    }
}
